//
//  RTLBaseRequest.h
//  RTLNetWorking
//
//  Created by 林波 on 16/7/8.
//  Copyright © 2016年 linbo1. All rights reserved.

//  网络框架(基础部分)

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>


@class RTLBaseRequest;


// Http请求的方法
typedef NS_ENUM(NSUInteger, RTLRequestMethod) {
    RTLRequestMethodGET = 0,
    RTLRequestMethodPOST,
    RTLRequestMethodPUT,
    RTLRequestMethodDELETE,
    RTLRequestMethodPATCH,
    RTLRequestMethodHEAD,
};

// 请求的SerializerType
typedef NS_ENUM(NSUInteger, RTLRequestSerializerType) {
    RTLRequestSerializerTypeHTTP = 0,
    RTLRequestSerializerTypeJSON,
};

// 返回的SerializerType
typedef NS_ENUM(NSUInteger, RTLResponseSerializerType) {
    RTLResponseSerializerTypeHTTP = 0,
    RTLResponseSerializerTypeJSON,
};


//block 返回
typedef NSError *(^RTLRequestResponseFilterCallback)(__kindof RTLBaseRequest *request, id responseObject);
typedef void(^RTLRequestSuccessCallback)(__kindof RTLBaseRequest *request, id responseObject);
typedef void(^RTLRequestFailureCallback)(__kindof RTLBaseRequest *request, NSError *error);
typedef void(^RTLRequestCancelCallback)(__kindof RTLBaseRequest *request);
typedef void(^RTLRequestCompletionBlock)(__kindof RTLBaseRequest *request);
typedef void(^RTLRequestProgressCallback)(NSProgress *progress);
typedef void(^RTLRequestConstructionCallback)(id<AFMultipartFormData> formData);


//代理方法
@protocol RTLBaseRequestDelegate <NSObject>

@optional

/**
 *  请求完成的方法（主要是用在链式请求和批量请求知道进行下一步请求）
 *
 *  @param request 请求接口
 */
- (void)requestFinished:(RTLBaseRequest *)request;

/**
 *  请求失败
 *
 *  @param request 请求接口
 */
- (void)requestFailed:(RTLBaseRequest *)request;

/**
 *  清除请求
 */
- (void)clearRequest;

@end

@protocol RTLRequestAccessory <NSObject>

@optional

/** 调用start */
- (void)requestWillStart:(id)request;

/** 请求任务重新连接 */
- (void)requestDidStart:(id)request;

/** 在 `successCallback` or `failureCallback` 前调用这个方法. */
- (void)requestWillStop:(id)request;

/* 在 `successCallback` or `failureCallback` 后调用这个方法. */
- (void)requestDidStop:(id)request;

@end



@interface RTLBaseRequest : NSObject


@property (nonatomic, strong, readonly) NSURLSessionDataTask *task;
@property (nonatomic, weak) id<RTLBaseRequestDelegate> delegate;
@property (nonatomic, strong) AFHTTPResponseSerializer <AFURLResponseSerialization> * responseSerializer;


- (void)addAccessory:(id<RTLRequestAccessory>)accessory;


/**
 *  请求配置
 */
// Tag
@property (nonatomic) NSInteger tag;

// 基础连接 例如：http://www.baidu.com
@property (nonatomic, copy) NSString *requestBaseURL;

// app的版本号
@property (nonatomic, copy) NSString *appVersion;

// 连接后面的参数   例如：user/getinfo／php
@property (nonatomic, copy)   NSString *requestURL;

// 请求方式默认为GET请求
@property (nonatomic, assign) RTLRequestMethod requestMethod;

// 请求超时时间
@property (nonatomic, assign) NSTimeInterval requestTimeoutInterval;

// 请求的默认头部
@property (nonatomic, strong) NSDictionary *requestDefaultHeader;

// 请求头部
@property (nonatomic, strong) NSDictionary *requestHeader;

// 请求参数
@property (nonatomic, strong) id requestParameters;


@property (nonatomic, copy)   RTLRequestConstructionCallback requestConstructionCallback;

// 请求的SerializerType
@property (nonatomic, assign) RTLRequestSerializerType requestSerializerType;

// 返回的SerializerType
@property (nonatomic, assign) BOOL showActivityIndicator;





/**
 *  请求响应配置
 */

@property (nonatomic, assign) RTLResponseSerializerType responseSerializerType;

// 返回的信息
@property (nonatomic, strong) id responseObject;

// 错误信息
@property (nonatomic, strong) NSError *error;

//返回状态的标示  例如：404错误
@property (nonatomic, assign, readonly) NSInteger responseStatusCode;

//返回头部信息
@property (nonatomic, strong, readonly) NSDictionary *responseHeader;


/**
 *  请求回调配置
 */
@property (nonatomic, copy) RTLRequestProgressCallback uploadProgressCallback;
@property (nonatomic, copy) RTLRequestProgressCallback downloadProgressCallback;
@property (nonatomic, copy) RTLRequestResponseFilterCallback responseFilterCallback;
@property (nonatomic, copy) RTLRequestSuccessCallback successCallback;
@property (nonatomic, copy) RTLRequestFailureCallback failureCallback;

//开始
- (void)start;

/**
 *  设置成功和错误回调（这里没有开始 如果要直接开始请调用下面那个方法）
 *
 *  @param success 成功回调
 *  @param failure 错误回调
 */
- (void)setSuccessCallback:(RTLRequestSuccessCallback)success failure:(RTLRequestFailureCallback)failure;

/**
 *  开始并设置成功和错误回调
 *
 *  @param success 成功回调
 *  @param failure 错误回调
 */
- (void)startRequestSuccessCallback:(RTLRequestSuccessCallback)success failureCallback:(RTLRequestFailureCallback)failure;



/**
 *  取消请求配置
 */

//判断是否可以取消
@property (nonatomic, assign, readonly) BOOL canCancel;

//取消回调
@property (nonatomic, copy) RTLRequestCancelCallback cancelCallback;

// 取消请求
- (void)cancel;

// 取消请求回调
- (void)cancelWithCallback:(RTLRequestCancelCallback)cancel;

// 请求成功
- (void)requestDidFinishSuccess;

// 请求失败
- (void)requestDidFinishFailure;


@end




//请求管理
@interface RTLRequestManager : NSObject

@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;


// 创建单例
+ (instancetype)sharedManager;

// 加入请求
- (void)addRequest:(RTLBaseRequest *)request;

// 移除请求
- (void)removeRequest:(RTLBaseRequest *)request;


@end

